<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="5" :xs="24">
        <el-card shadow="always">
          <org-tree ref="orgTree" modelType="'dept','community'" @nodeClick="handleNodeClick" @firstNodeClick="handleFirstNodeClick"/>
        </el-card>
      </el-col>
      <el-col :span="19" :xs="24">
        <el-card shadow="always">
          <el-form :model="queryParams" ref="queryForm" v-show="showSearch" label-width="68px" class="form-flex">
            <el-row :gutter="20">
              <el-col :span="6" :xs="24">
                <el-form-item label="地点名称" prop="placeName">
                  <el-input
                    v-model="queryParams.placeName"
                    placeholder="请输入地点名称"
                    clearable
                    size="small"
                    @keyup.enter.native="handleQuery"/>
                </el-form-item>
              </el-col>
              <el-col :span="6" :xs="24">
                <el-form-item>
                  <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                  <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>

          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['waitress:place:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['waitress:place:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['waitress:place:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport" v-hasPermi="['waitress:place:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>

          <el-table v-loading="loading" :data="placeList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="地点名称" align="center" prop="placeName" />
            <el-table-column label="地点编号" align="center" prop="placeOdd" />
            <el-table-column label="巡检要求" align="center" prop="cruiserAsk"  show-overflow-tooltip/>
            <el-table-column label="创建时间" align="center" prop="createTime" />
            <el-table-column label="状态" align="center" prop="placeState" >
              <template slot-scope="scope">
                {{ dict.label.place_state[scope.row.placeState] }}
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  v-if="scope.row.placeState=='10'"
                  icon="el-icon-edit"
                  @click="handlestop(scope.row)"
                  v-hasPermi="['waitress:place:updStop']"
                >停用</el-button>
                <el-button
                  size="mini"
                  type="text"
                  v-if="scope.row.placeState=='20'"
                  icon="el-icon-edit"
                  @click="handleStart(scope.row)"
                  v-hasPermi="['waitress:place:updStop']"
                >启用</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"/>
        </el-card>
      </el-col>
    </el-row>

    <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body v-drag-fullscreen>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="8" :xs="24">
            <el-form-item label="所属小区" prop="communityId">
              <el-select v-model="form.communityId" style="width: 100%;" placeholder="请选择所属小区">
                <el-option
                  v-for="dict in communityOptions"
                  :key="dict.id"
                  :label="dict.communityName"
                  :value="dict.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8" :xs="24">
            <el-form-item label="地点名称" prop="placeName">
              <el-input v-model="form.placeName" placeholder="请输入地点名称" :disabled="disabled"/>
            </el-form-item>
          </el-col>
          <el-col :span="8" :xs="24">
            <el-form-item label="地点编号" prop="placeOdd">
              <el-input v-model="form.placeOdd" placeholder="请输入地点编号" :disabled="disabled"/>
            </el-form-item>
          </el-col>
          <el-col :span="24" :xs="24">
            <el-form-item label="巡检要求" prop="cruiserAsk">
              <el-input v-model="form.cruiserAsk" type="textarea" placeholder="请输入巡检内容" :disabled="disabled"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { listMixin } from "@/utils/mixins/listMixin";
import { listPlace, delPlace, exportPlace, getPlace , addPlace, updatePlace,updStop } from "@/api/waitress/place";
import PlaceForm from "./form.vue"
import { listCommunity} from "@/api/property/community";
export default {
  name: "Place",
  components:{ PlaceForm },
  mixins:[ listMixin ],
  dicts: [   'place_state',   ],
  data() {
    return {
      // 地点管理表格数据
      placeList: [],
      currentNode:{},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //是否禁用
      disabled: false,
      // 表单参数
      form: {},
      communityOptions:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        communityId: null,
        placeName: null,
        placeOdd: null,
        cruiserAsk: null,
        ancestors: null,
        deptId: null,
        nodeId:null,
      },
      // 表单校验
      rules: {
        communityId: [
          { required: true, message: "小区ID不能为空", trigger: "blur" }
        ],
        placeName: [
          { required: true, message: "地点名称不能为空", trigger: "blur" }
        ],
        cruiserAsk: [
          { required: true, message: "巡逻要求不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    // this.getList();
    listCommunity().then(res=>{
      this.communityOptions = res.rows;
    })
  },
  methods: {
    /** 查询地点管理列表 */
    getList() {
      this.loading = true;
      listPlace(this.queryParams).then(response => {
        this.placeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        communityId: null,
        placeName: null,
        placeOdd: null,
        cruiserAsk: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        ancestors: null,
        deptId: null,
        placeState:null,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePlace(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.cancel()
            });
          } else {
            addPlace(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.cancel()
            });
          }
        }
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      if(this.currentNode.modelType == 'community'){
        this.form.communityId = this.currentNode.id
        this.open = true;
      }else {
        this.open = true;
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids
      const option = {
          "type":"UPDATE",
          "id": id
      }
      getPlace(option.id).then(response => {
        this.form = response.data;
        if(option.type == 'VIEW'){
          this.title = "查看地点管理";
        }else{
          this.title = "修改地点管理";
        }
        this.open = true;
      });
    },

    handlestop(row){
      const id = row.id || this.ids
      this.form.id=id;
      this.form.placeState=20;
      updStop(this.form).then(response => {
        this.msgSuccess("停用成功");
        this.cancel()
      });
    },
    handleStart(row){
      const id = row.id || this.ids
      this.form.id=id;
      this.form.placeState=10;
      updStop(this.form).then(response => {
        this.msgSuccess("启动成功");
        this.cancel()
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除地点管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPlace(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    handleNodeClick(node){
      this.currentNode = node;
      this.queryParams.nodeId = node.id
      this.getList()
    },
    handleFirstNodeClick(node){
      this.currentNode = node;
      this.queryParams.nodeId = node.id;
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.$emit("cancel");
      this.getList();
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有地点管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportPlace(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
